package com.torry.workflow.service.web.controller;

import com.torry.workflow.api.user.api.PermApi;
import com.torry.workflow.api.user.entity.Perm;
import com.torry.workflow.common.base.result.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/perm")
public class PermController {
    @DubboReference
    private PermApi permApi;

    @GetMapping("allPerms")
    public Result allPerms() {
        List<Perm> perms = permApi.allPerms();
        return Result.selectSuccess(perms);
    }

    @PostMapping("save")
    public Result savePermInfo(@RequestBody Perm perm) {
        permApi.savePerm(perm);
        return Result.saveSuccess();
    }

    @DeleteMapping("delete/{ids}")
    public Result deletePermByIds(@PathVariable String ids) {
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        List<Integer> idList = strings.stream().map(id -> Integer.parseInt(id)).collect(Collectors.toList());
        permApi.deletePermByIds(idList);
        return Result.delSuccess();
    }
}
